package com.xhsj.user.hr.process.processDefinition.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.hr.base.BaseService;
import com.xhsj.user.hr.process.hrLeaveProcess.query.FirstProcessNodeIdQuery;
import com.xhsj.user.hr.process.hrLeaveProcess.query.WorkFlowNodeQuery;
import com.xhsj.user.hr.process.processDefinition.entity.TbGlobalWorkFlowNode;
import com.xhsj.user.hr.process.processDefinition.mapper.TbGlobalWorkFlowNodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 流程设计-流程节点 服务实现类
 * </p>
 *
 * @author liuqinghua
 * @since Aug 28, 2020 4:40:10 PM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbGlobalWorkFlowNode")
public class TbGlobalWorkFlowNodeService extends BaseService<TbGlobalWorkFlowNodeMapper, TbGlobalWorkFlowNode> {
    @Autowired
    private TbGlobalWorkFlowNodeMapper dao;

	/**
	 * 保存
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
    @Override
    //@CacheEvict(cacheNames="TbGlobalWorkFlowNode", allEntries=true)
    public boolean save(TbGlobalWorkFlowNode entity) {
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbGlobalWorkFlowNode getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
    @Override
    //@CacheEvict(cacheNames="TbGlobalWorkFlowNode", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 批量删除数据
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

	/**
	 * 分页查询数据
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
    //@Cacheable(key = "#data",sync = true)
    public IPage<TbGlobalWorkFlowNode> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbGlobalWorkFlowNode entity = JSON.parseObject(JSON.toJSONString(data), TbGlobalWorkFlowNode.class);
        Page<TbGlobalWorkFlowNode> page = new Page(currentPage,pageSize);
        QueryWrapper<TbGlobalWorkFlowNode> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author liuqinghua
	 * @date Aug 28, 2020 4:40:10 PM
	 */
    @Override
    //@Cacheable(value = "TbGlobalWorkFlowNode",sync = true)
    public List<TbGlobalWorkFlowNode> selectAll() {
        return super.selectAll();
    }

    /**
     * 删除流程设计-流程节点
     * @author liuqinghua
     * @date Aug 28, 2020 4:40:10 PM
     */
    public int deleteByBusinessId(Long businessId) {
       return dao.deleteByBusinessId(businessId);
    }

    /**
     * 查询初始流程节点
     * @author liuqinghua
     * @date Aug 28, 2020 4:40:10 PM
     */
    public List<FirstProcessNodeIdQuery>  firstProcessNodeId(int applyType, long organizationId) {
        return dao.firstProcessNodeId(applyType,organizationId);
    }

    /**
     *  流程节点
     * @author liuqinghua
     * @date Aug 28, 2020 4:40:10 PM
     */
    public List<WorkFlowNodeQuery> nodeList(Long businessId) {
        return dao.getNodeList(businessId);
    }

    /**
     * 查询数据流程的最新节点
     * @author 苏小林
     * @date 2020/9/7 10:00
     * @param: businessId
     * @param: dataId
     * @return: java.lang.Long
     */
    public Long findCurrentNodeId(Long businessId, Long dataId) {
        return dao.findCurrentNodeId(businessId,dataId);
    }

    /**
     * 查询当前节点的角色
     * @author 苏小林
     * @date 2020/9/11 17:07
     * @param: businessId
     * @param: nodeId
     * @return: java.lang.String
     */
    public String getRoleByBusinessIdAndNodeId(Long businessId, Long nodeId) {
        return dao.getRoleByBusinessIdAndNodeId(businessId,nodeId);
    }
}
